import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';
import 'package:wk_robot2/app/const/AppConstUrl.dart';

import '../../../routes/app_pages.dart';
import '../../../style/app_colors.dart';
import '../../../widget/CommonWidgets.dart';
import '../controllers/about_app_controller.dart';

typedef ItemWidgetTapCallback = void Function(String value0, String value1);

class AboutAppView extends GetView<AboutAppController> {
  AboutAppView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: AppColors.app_base_bgcolor_color,
        appBar: CommonWidgets.buildAppBar(context, '关于'),
        body: SizedBox(
          width: MediaQuery.of(context).size.width,
          child: Column(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                margin: EdgeInsets.only(top: 103.h),
                width: 180.w,
                height: 180.h,
                child: Image.asset(
                  'assets/images/module_about_app_icon.webp',
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 22.h),
                child: Text(
                  "悟空机器人",
                  style: TextStyle(fontSize: 34.sp),
                ),
              ),
              _buildAppVersionWidget(),
              _buildAboutContentWidget(),
              Spacer(),
              _buildAppCopyrightWidget("ICP 备案号：粤ICP备1303654"),
              _buildAppCopyrightWidget("优必选科技有限公司 版权所有"),
              Container(
                margin: EdgeInsets.only(bottom: 50.h),
                child: _buildAppCopyrightWidget(
                    "Copyright ©2018 Ubtech Robotics Corp. All rights reserved"),
              ),
            ],
          ),
        ));
  }

  Widget _buildAppVersionWidget() {
    return Container(
        margin: EdgeInsets.only(top: 10.h),
        child: Obx(
          () => Text(
            controller.apkVersion.value,
            style: TextStyle(fontSize: 26.sp, color: AppColors.secondaryText),
          ),
        ));
  }

  Widget _buildAboutContentWidget() {
    return Container(
      margin: EdgeInsets.all(40.h),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(16)),
          color: Colors.white),
      child: Column(
        children: [
          _buildColumnItemWidget('隐私政策',
              itemUrl: AppConstUrl.LOGIN_PRIVACY_POLICY_URL,
              onTap: onClickItemToWebPageTask),
          _buildItemDividerWidget(),
          _buildColumnItemWidget('用户协议',
              itemUrl: AppConstUrl.LOGIN_USER_AGREEMENT_URL,
              onTap: onClickItemToWebPageTask),
          _buildItemDividerWidget(),
          _buildColumnItemWidget('用户信息收集清单'),
          _buildItemDividerWidget(),
          _buildColumnItemWidget('系统权限调用列表'),
          _buildItemDividerWidget(),
          _buildColumnItemWidget('第三方收集使用个人信息列表'),
          //_buildItemDividerWidget(),
        ],
      ),
    );
  }

  Widget _buildAppCopyrightWidget(String copyRight,
      {GestureTapCallback? onTap}) {
    return GestureDetector(
        onTap: onTap,
        child: Text(
          maxLines: 2,
          textAlign: TextAlign.center,
          copyRight,
          style: TextStyle(
            fontSize: 22.sp,
            color: AppColors.secondaryText,
          ),
        ));
  }

  void onClickItemToWebPageTask(String title, String url) {
    if (url.isNotEmpty) {
      Map<String, String> param = HashMap();
      param["title"] = title;
      param["url"] = url;
      Get.toNamed(Routes.WEBVIEW, parameters: param);
    }
  }

  Widget _buildColumnItemWidget(String itemTitle,
      {String? itemUrl, ItemWidgetTapCallback? onTap}) {
    return GestureDetector(
      onTap: () {
        if (onTap != null) {
          onTap.call(itemTitle, itemUrl ?? "");
        }
      },
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 22.h, horizontal: 30.w),
        child: _buildTextNextIconRowWidget(itemTitle),
      ),
    );
  }

  Widget _buildItemDividerWidget() {
    return Container(
        padding: EdgeInsets.only(left: 30.w, right: 30.w),
        child: CommonWidgets.getMyDivider());
  }

  Widget _buildTextNextIconRowWidget(String title, {String? assetName}) {
    String name = assetName ?? 'assets/images/ic_arrow.webp';
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          title,
          style: TextStyle(fontSize: 32.sp),
        ),
        Image.asset(
          name,
          width: 30.w,
          height: 30.h,
        ),
      ],
    );
  }
}
